#include "Line2D.h"
#include "../GameLib.h"
#include "../../OpenGL.h"
#include "../Logger.h"


Line2D::Line2D(){}
Line2D::Line2D(Point2D &newA, Point2D &newB){
    set(newA,newB);
}
Line2D::Line2D(double xa, double ya, double xb, double yb){
    set(xa,ya,xb,yb);
}
Line2D::~Line2D(){}
Line2D& Line2D::set(Point2D &newA,Point2D &newB){
    a=newA;
    b=newB;
    Str temp;
    mainLog.print(Str()<<"The Line's point a is set to: "<<a.toString(temp)<<" and the Line's point b is set to: "<<b.toString(temp));
    return *this;
}
Line2D& Line2D::set(double xa,double ya,double xb,double yb){
    a.set(xa,ya);
    b.set(xb,yb);
    Str temp;
    mainLog.print(Str()<<"The Line's point a is set to: "<<a.toString(temp)<<" and the Line's point b is set to: "<<b.toString(temp));
    return *this;
}
double Line2D::length(){
    return a.distance(b);
}
double Line2D::slope(){
    return (b.getY()-a.getY())/(b.getX()-a.getX());
}
double Line2D::intercept(){
    return a.getY()-(slope()*a.getX());
}
void Line2D::mb(double &m, double &b){
    m=slope();
    b=a.getY()-(m*a.getX());
}
void Line2D::draw(double depth){
    Str temp;
    Str debug;
    debug << "Trying to draw from : ";
    a.toString(temp);
    debug << temp << ", " << depth << " ~ ";
    b.toString(temp);
    debug << temp << ", " << depth;
    mainLog.print(debug);
    glBegin(GL_LINES);
    glVertex3f((float)a.getX(),(float)a.getY(),(float)depth);
    glVertex3f((float)b.getX(),(float)b.getY(),(float)depth);
    glEnd();
}
void Line2D::draw_width(double depth, double width){
    double oldLine2DWidth;
    oldLine2DWidth=gameLib.getLineWidth();
    gameLib.setLineWidth(width);
    draw(width);
    gameLib.setLineWidth(oldLine2DWidth);
}
void Line2D::draw_color(double depth, int hexColor){
    float c[4];
    gameLib.getColorA(c);
    gameLib.setColor(hexColor);
    draw(depth);
    gameLib.setColorA(c);
}
void Line2D::draw_widthColor(double depth, double width, int hexColor){
    float c[4];
    gameLib.getColorA(c);
    gameLib.setColor(hexColor);
    draw_width(depth,width);
    gameLib.setColorA(c);
}